<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-type" content="text/html; charset=utf-8">
		<title>Static Master/Detail Pattern -- Multiple Address Detail example</title>

		<script src="../../../dojo/dojo.js"
				type="text/javascript"
				data-dojo-config="parseOnLoad: false, isDebug: true, mvc: {debugBindings: true}">
		</script>
		<style type="text/css">
			@import "css/app-format.css";
			@import "../../../dijit/themes/claro/claro.css";
		</style>
		<script type="text/javascript">
		var model1, model2, model3;
 
		require([
			'dojo/parser',
			"dojo/_base/lang",
			'dojo/ready',
			'dojo/when',
			"dojo/Stateful",
			"dojox/mvc/getStateful",
			"dojox/mvc/getPlainValue",
			"dojo/store/Memory",
			'dijit/registry',			
			'dijit/form/TextBox',
			'dijit/form/Button',
			'dojox/mvc/Group',
			'dojox/mvc/Repeat',
			'dojox/mvc/Output'
			], function(parser, lang, ready, when, Stateful, getStateful, getPlainValue, Memory, registry){
				
				model1Data =
				[{
					"Query" : "Engineers1",
					"Results" : [
						{
							"First"	  : "Anne1",
							"Last"	  : "Ackerman1"
						},
						{
							"First"	  : "Ben1",
							"Last"	  : "Beckham1"
						},
						{
							"First"	  : "Chad1",
							"Last"	  : "Chapman1"
						}
					]
				}];
				model2Data =
				[{
					"Query" : "Engineers2",
					"Results" : [
						{
							"First"	  : "Anne2",
							"Last"	  : "Ackerman2"
						},
						{
							"First"	  : "Ben2",
							"Last"	  : "Beckham2"
						}
					]
				}];
				model3Data =
				[{
					"Query" : "Engineers3",
					"Results" : [
						{
							"First"	  : "",
							"Last"	  : ""
						}
					]
				}];

				var memStore1 = new Memory({data : model1Data});
				when(memStore1.query(), function(data){
					model1 = new Stateful({current: getStateful(data)[0]});
				});

				var memStore2 = new Memory({data : model2Data});
				//model2 = mvc.newStatefulModel({ store : memStore2 })[0];
				when(memStore2.query(), function(data){
					model2 = new Stateful({current: getStateful(data)[0]});
				});
				var memStore3 = new Memory({data : model3Data});
				//model3 = mvc.newStatefulModel({ store : memStore3 })[0];
				when(memStore3.query(), function(data){
					//model3 = getStateful(data)[0];
					model3 = new Stateful({current: getStateful(data)[0]});
				});

				replaceData = function(dest, src){
					src = getPlainValue(src);
					dest.set("current", getStateful(src));
				};
				
					
				// when "dojo/ready" is ready call parser.parse
				ready(function(){
					parser.parse();
				});
			});

		</script>
	</head>
	<body class="claro">
		<script type="dojo/require">at: "dojox/mvc/at"</script>
		<div id="wrapper">
			<div id="header">
				<div id="navigation"></div>
				<div id="headerInsert">
					<h1>Input Ouput Sync</h1>
					<h2>Data Binding Example</h2>
				</div>
			</div>
			<div id="main">
				<div id="leftNav"></div>
				<div id="mainContent">
  
<h4>This will swap which model is used for the textboxes, so updates will effect the output for that model</h4>
					<div class="row">
						<button type="button" data-dojo-type="dijit.form.Button" data-dojo-props="onClick: function(){dijit.byId('outergroupId').set('target',at(model1, 'current'));}">Swap Model1</button>
						<button type="button" data-dojo-type="dijit.form.Button" data-dojo-props="onClick: function(){dijit.byId('outergroupId').set('target',at(model2, 'current'));}">Swap Model2</button>
						<button type="button" data-dojo-type="dijit.form.Button" data-dojo-props="onClick: function(){dijit.byId('outergroupId').set('target',at(model3, 'current'));}">Swap Model3</button>
					</div>

					<h4>Replace Model 1 data with original data from the models listed below, this should keep the bindings to the Model1 Output</h4>
					<div class="row" style="width: 740px">
	  					<button type="button" data-dojo-type="dijit.form.Button" data-dojo-props="onClick: function(){replaceData(model1, model1Data[0]);}">Replace with Orig. Model1 data</button>
						<button type="button" data-dojo-type="dijit.form.Button" data-dojo-props="onClick: function(){replaceData(model1, model2Data[0]);}">Replace with Orig. Model2 data</button>
						<button type="button" data-dojo-type="dijit.form.Button" data-dojo-props="onClick: function(){replaceData(model1, model3Data[0]);}">Replace with Orig. Model3 data</button>
					</div>
					<h4>Replace Model 1 data with the current model data listed below, this should keep the bindings to the Model1 Output</h4>
					<div class="row" style="width: 540px">						
						<button type="button" data-dojo-type="dijit.form.Button" data-dojo-props="onClick: function(){replaceData(model1, model2.current);}">Replace with Model2 data</button>
						<button type="button" data-dojo-type="dijit.form.Button" data-dojo-props="onClick: function(){replaceData(model1, model3.current);}">Replace with Model3 data</button>
					</div>
					<div id="outergroupId"	 data-dojo-type="dojox.mvc.Group" data-dojo-props="target: at(model1, 'current')">
						<div id="repeatId" data-dojo-type="dojox.mvc.Repeat" 
											data-dojo-props="children: at('rel:', 'Results')">
							<div class="row" data-dojo-type="dojox.mvc.Group" 
											data-dojo-props="target: at('rel:${this.index}')">
								<label class="cell" for="nameInput${this.index}">Name:</label>
								<input class="cell" data-dojo-type="dijit.form.TextBox" id="nameInput${this.index}" 
									data-dojo-props="value: at('rel:', 'First')">
							</div>
						</div>
					</div>
					<div class="row">
						<span>Model1 Output is ==&gt;  </span>
						<div id="outergroupId12"	 data-dojo-type="dojox.mvc.Group" data-dojo-props="target: at(model1, 'current')">
							<div id="repeatId12" data-dojo-type="dojox.mvc.Repeat" 
											data-dojo-props="children: at('rel:', 'Results')">
								<div data-dojo-type="dojox.mvc.Group" 
											data-dojo-props="target: at('rel:${this.index}')">
									<span>Model1 Name${this.index} is</span>
									<span id="firstnameOutput12${this.index}" data-dojo-type="dojox.mvc.Output"
										data-dojo-props="value: at('rel:', 'First')"></span>
								</div>
							</div>
						</div>
					</div>
					<div class="row">
						<span>Model2 Output is ==&gt;  </span>
						<div id="outergroupId22"	 data-dojo-type="dojox.mvc.Group" data-dojo-props="target: at(model2, 'current')">
							<div id="repeatId22" data-dojo-type="dojox.mvc.Repeat" 
											data-dojo-props="children: at('rel:', 'Results')">
								<div data-dojo-type="dojox.mvc.Group" 
											data-dojo-props="target: at('rel:${this.index}')">
									<span>Model2 Name${this.index} is</span>
									<span id="firstnameOutput22${this.index}" data-dojo-type="dojox.mvc.Output"
										data-dojo-props="value: at('rel:', 'First')"></span>
								</div>
							</div>
						</div>
					</div>
					<div class="row">
						<span>Model3 Output is ==&gt;  </span>
						<div id="outergroupId23"	 data-dojo-type="dojox.mvc.Group" data-dojo-props="target: at(model3, 'current')">
							<div id="repeatId23" data-dojo-type="dojox.mvc.Repeat" 
											data-dojo-props="children: at('rel:', 'Results')">
								<div data-dojo-type="dojox.mvc.Group" 
											data-dojo-props="target: at('rel:${this.index}')">
									<span>Model3 Name${this.index} is</span>
									<span id="firstnameOutput23${this.index}" data-dojo-type="dojox.mvc.Output"
										data-dojo-props="value: at('rel:', 'First')"></span>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</body>
</html>
